import { View } from "../../../mvc/core/View";
import { IClean } from "../../../../com/gengine/core/IClean";
import { SEntityInfo } from "../../../../Message/BroadCast/SEntityInfo";
import { BaseProgressBar } from "../common/BaseProgressBar";
import { GLabel } from "../../../../com/mui/controls/GLabel";
import { FilterConst } from "../../../component/gconst/FilterConst";
import { GTextFormat } from "../../../common/GTextFormat";
import { FontUtil } from "../../../common/font/FontUtil";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { ControlSlider } from "./ControlSlider";
import { SliderEvent } from "../../../../fl/events/SliderEvent";
import { Language } from "../../../../extend/language/Language";
import { DataEvent } from "../../events/DataEvent";
import { ThingUtil } from "../../scene/layer/utils/ThingUtil";
import { UserPlayer } from "../../scene/player/entity/UserPlayer";
import { EntityUtil } from "../../scene/layer/utils/EntityUtil";
import { PlayerUtil } from "../common/util/PlayerUtil";
import { Cache } from "../../cache/Cache";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { LayerManager } from "../../manager/LayerManager";
import { Tooltip } from "../common/tooltip/Tooltip";
import { BuffPanel } from "./BuffPanel";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { AvatarEvent } from "./AvatarEvent";
type int = number;
//class BaseAvatar
    
    export  class BaseAvatar extends View implements IClean
    {
        constructor(arg1: SEntityInfo=null)
        {
            super();
            if (arg1) 
            {
                this.entityInfo = arg1;
                this.init();
            }
            return;
        }

        /* internal  */createProgressBars(arg1: string, arg2: string, arg3: int, arg4: int): BaseProgressBar
        {
            var loc1;
            (loc1 = new BaseProgressBar("")).isScale = true;
            loc1.progressBarClass = arg2;
            loc1.setValue(arg3, arg4);
            loc1.setLabelPosition(0, -4);
            return loc1;
        }

        public createNameLabel(): void
        {
            this.nameLabel = new GLabel();
            this.nameLabel.name = "name";
            this.nameLabel.x = 90;
            this.nameLabel.y = 18;
            this.nameLabel.width = 120;
            this.nameLabel.text = this.entityInfo.name;
            this.nameLabel.textField.filters = [FilterConst.nameGlowFilter];
            this.nameLabel.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this.nameLabel.setStyle("textFormat", new GTextFormat(FontUtil.songtiName, 12, 16777215));
            this.addChild(this.nameLabel);
            return;
        }

        public createLevelLabel(): void
        {
            this.levelLabel = new GLabel();
            this.levelLabel.name = "level";
            this.levelLabel.x = 36;
            this.levelLabel.y = 18;
            this.levelLabel.text = this.entityInfo.level.toString();
            this.levelLabel.textField.filters = [FilterConst.glowFilter];
            this.levelLabel.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this.levelLabel.setStyle("textFormat", new GTextFormat(FontUtil.songtiName, 12, 16777215));
            this.addChild(this.levelLabel);
            return;
        }

        public createAvatar(): void
        {
            this.avatarBitmap = new Object /* flash.display.Bitmap */();
            if (!(this.avatarName == null) && !(this.avatarName == "")) 
            {
                LoaderManager.instance.load(this.avatarName, this.onAvatarLoaded);
            }
            else 
            {
                this.avatarBitmap.bitmapData = null;
            }
            if (this.avatarBitmap) 
            {
                this.avatarBitmap.x = 17;
                this.avatarBitmap.y = 8;
                this.addChild(this.avatarBitmap);
            }
            return;
        }

        public createLifeSlider(arg1: string="", arg2: string="ControlBarBig"): void
        {
            this.lifeSlider = new ControlSlider(arg2, arg1);
            this.lifeSlider.x = 87;
            this.lifeSlider.y = 37;
            this.lifeSlider.width = 110;
            this.lifeSlider.thumbData = this.getLifeSetToolTip(this.lifeSlider.value);
            this.lifeSlider.addEventListener(SliderEvent.CHANGE, this.onLifeSetChangeHandler);
            this.addChild(this.lifeSlider);
            return;
        }

        /* internal  */onLifeSetChangeHandler(arg1: SliderEvent): void
        {
            var loc1=arg1.target.value;
            this.lifeSlider.thumbData = this.getLifeSetToolTip(loc1);
            return;
        }

        protected onHealthBarRollOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._isHealthOver = true;
            this.ResetLabelTipVisible();
            this.labelTip.data = "<font color=\'#ffffff\'>" + Language.getStringByParam(50026, this.entityInfo.life, this.entityInfo.maxLife) + "</font>";
            return;
        }

        public getLifeSetToolTip(arg1: int): string
        {
            return Language.getStringByParam(50028, arg1);
        }

        public getManaSetToolTip(arg1: int): string
        {
            return Language.getStringByParam(50029, arg1);
        }

        public createManaSlider(arg1: string="", arg2: string="ControlBarBig"): void
        {
            this.manaSlider = new ControlSlider(arg2, arg1);
            this.manaSlider.x = 87;
            this.manaSlider.y = 50;
            this.manaSlider.width = 110;
            this.manaSlider.thumbData = this.getManaSetToolTip(this.manaSlider.value);
            this.manaSlider.addEventListener(SliderEvent.CHANGE, this.onManaSetChangeHandler);
            this.addChild(this.manaSlider);
            return;
        }

        /* internal  */onManaSetChangeHandler(arg1: SliderEvent): void
        {
            var loc1=this.manaSlider.value;
            this.manaSlider.thumbData = this.getManaSetToolTip(loc1);
            return;
        }

        /* internal  */sliderUp(arg1: DataEvent): void
        {
            var loc1=this.lifeSlider.value / 100;
            var loc2=this.manaSlider.value / 100;
            return;
        }

        public updateAvatarUI(arg1: SEntityInfo): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            this.entityInfo = arg1;
            if (arg1) 
            {
                if (this.nameLabel) 
                {
                    loc3 = ThingUtil.entityUtil.getEntity(arg1.entityId);
                    loc1 = arg1.name;
                    if (loc3 && loc3 is UserPlayer) 
                    {
                        if (!EntityUtil.isSameServerByRole(arg1.entityId)) 
                        {
                            loc1 = PlayerUtil.getProxyAndNameByEntityIdAndName(arg1.entityId, arg1.name);
                        }
                    }
                    this.nameLabel.text = loc1;
                }
                this.levelLabel.text = arg1.level.toString();
                this.healthBar.setValue(arg1.life, arg1.maxLife);
                this.magicBar.setValue(arg1.mana, arg1.maxMana);
                loc2 = arg1.entityId;
                if (loc2) 
                {
                    if (EntityUtil.toString(loc2) != EntityUtil.toString(Cache.instance.role.entityInfo.entityId)) 
                    {
                        this.updateBufferByTSateIdArray(arg1.buffers);
                    }
                }
            }
            return;
        }

        public updateBufferByBuffInfoArray(arg1: Array<any>): void
        {
            this.buffPanel.updateBufferByBuffInfoArray(arg1);
            return;
        }

        public destory(): void
        {
            var loc3=null;
            var loc1=[this.healthBar, this.magicBar, this.avatarBg, this.nameLabel, this.levelLabel, this.closeBtn];
            var loc2=0;
            while (loc2 < loc1.length) 
            {
                if (null != loc1[loc2]) 
                {
                    if (this.contains(loc1[loc2])) 
                    {
                        this.removeChild(loc1[loc2]);
                    }
                    loc3 = flash.utils.getQualifiedClassName(loc1[loc2]);
                    if (loc3.indexOf("Bitmap") == -1) 
                    {
                        loc1[loc2] = null;
                    }
                    else 
                    {
                        loc1[loc2].bitmapData.dispose();
                    }
                }
                ++loc2;
            }
            return;
        }

        public get mainBgName(): string
        {
            return this._mainBgName;
        }

        public set mainBgName(arg1: string)
        {
            this.avatarBg.bitmapData = GlobalClass.getBitmapData(arg1);
            this._mainBgName = arg1;
            return;
        }

        public get avatarName(): string
        {
            return this._avatarName;
        }

        public set avatarName(arg1: string)
        {
            if (arg1 != this._avatarName) 
            {
                this.avatarBitmap.bitmapData = null;
                LoaderManager.instance.removeResourceEvent(this._avatarName, this.onAvatarLoaded);
                LoaderManager.instance.load(arg1, this.onAvatarLoaded);
                this._avatarName = arg1;
            }
            return;
        }

        public init(): void
        {
            this.createLabelTip();
            this.createBg();
            this.createAvatar();
            this.createHealthBar(ImagesConst.Shengmingtiao, 89, 39);
            this.createMagicBar(ImagesConst.Mofatiao, 89, 51);
            this.createNameLabel();
            this.createLevelLabel();
            return;
        }

        /* internal  */addOrRemoveLabelToolTip(arg1: Boolean=false): void
        {
            if (this.labelTip) 
            {
                if (arg1) 
                {
                    LayerManager.toolTipLayer.addChild(this.labelTip);
                }
                else if (this.labelTip.parent) 
                {
                    this.labelTip.parent.removeChild(this.labelTip);
                }
            }
            return;
        }

        public createLabelTip(): void
        {
            this.labelTip = new Tooltip();
            this.labelTip.mouseEnabled = false;
            this.labelTip.mouseChildren = false;
            return;
        }

        public createBg(arg1: string="RoleAvatarbg"): void
        {
            this.avatarBg = new Object /* flash.display.Bitmap */();
            this.avatarBg.bitmapData = GlobalClass.getBitmapData(arg1);
            this.addChild(this.avatarBg);
            return;
        }

        public createEffects(arg1: int, arg2: int, arg3: int): void
        {
            this.buffPanel = new BuffPanel(arg1);
            this.buffPanel.x = arg2;
            this.buffPanel.y = arg3;
            this.addChild(this.buffPanel);
            return;
        }

        public createHealthBar(arg1: string, arg2: int, arg3: int): void
        {
            this.healthBar = this.createProgressBars("", arg1, this.entityInfo.life, this.entityInfo.maxLife);
            this.healthBar.x = arg2;
            this.healthBar.y = arg3;
            if (this.isShowHealthLabelTip) 
            {
                this.healthBar.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.onHealthBarRollOver);
                this.healthBar.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.onBarRollOut);
                this.healthBar.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onBarMouseMove);
                this.healthBar.addEventListener(flash.events.MouseEvent.CLICK, this.onHealthBarClick);
            }
            this.healthBar.showProgressText = this.isShowHealthText;
            this.healthBar.isScale = true;
            this.addChild(this.healthBar);
            return;
        }

        protected onAvatarLoaded(arg1: ImageInfo): void
        {
            this.avatarBitmap.bitmapData = arg1.bitmapData;
            return;
        }

        public clean(): void
        {
            if (this.labelTip && this.labelTip.parent) 
            {
                this.labelTip.parent.removeChild(this.labelTip);
            }
            this.hide();
            if (this.lifeSlider) 
            {
                this.lifeSlider.clean();
            }
            if (this.manaSlider) 
            {
                this.manaSlider.clean();
            }
            if (this.buffPanel) 
            {
                this.buffPanel.clean();
            }
            return;
        }

        public updateBufferByTSateIdArray(arg1: Array<any>): void
        {
            this.buffPanel.updateBufferByTSateIdArray(arg1);
            return;
        }

        protected onManaBarRollOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._isManaOver = true;
            this.ResetLabelTipVisible();
            this.labelTip.data = "<font color=\'#ffffff\'>" + Language.getStringByParam(50027, this.entityInfo.mana, this.entityInfo.maxMana) + "</font>";
            return;
        }

        protected onBarRollOut(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._isHealthOver = false;
            this._isManaOver = false;
            this.ResetLabelTipVisible();
            return;
        }

        protected onBarMouseMove(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.ResetLabelTipVisible();
            this.labelTip.x = arg1.stageX + 15;
            this.labelTip.y = arg1.stageY + 15;
            return;
        }

        protected onMagicBarMouseClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.dispatchEvent(new DataEvent(AvatarEvent.ChangeManaSet, arg1.localX, false, true));
            return;
        }

        protected onHealthBarClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.dispatchEvent(new DataEvent(AvatarEvent.ChangeLifeSet, arg1.localX, false, true));
            return;
        }

        /* internal  */ResetLabelTipVisible(): void
        {
            if ((this._isHealthOver || this._isManaOver) && !this.lifeSlider._isDown && !this.manaSlider.isDown) 
            {
                this.addOrRemoveLabelToolTip(true);
            }
            else 
            {
                this.addOrRemoveLabelToolTip(false);
            }
            return;
        }

        public createMagicBar(arg1: string, arg2: int, arg3: int): void
        {
            this.magicBar = this.createProgressBars("", arg1, this.entityInfo.mana, this.entityInfo.maxMana);
            this.magicBar.x = arg2;
            this.magicBar.y = arg3;
            if (this.isShowManaLabelTip) 
            {
                this.magicBar.addEventListener(flash.events.MouseEvent.ROLL_OVER, this.onManaBarRollOver);
                this.magicBar.addEventListener(flash.events.MouseEvent.ROLL_OUT, this.onBarRollOut);
                this.magicBar.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.onBarMouseMove);
                this.magicBar.addEventListener(flash.events.MouseEvent.CLICK, this.onMagicBarMouseClick);
            }
            this.magicBar.isScale = true;
            this.magicBar.showProgressText = this.isShowManaText;
            this.addChild(this.magicBar);
            return;
        }

        protected /* var */avatarBitmap: Object /* flash.display.Bitmap */;

        protected /* var */healthBar: BaseProgressBar;

        protected /* var */magicBar: BaseProgressBar;

        protected /* var */avatarBg: Object /* flash.display.Bitmap */;

        protected /* var */nameLabel: GLabel;

        protected /* var */levelLabel: GLabel;

        protected /* var */lifeSlider: ControlSlider;

        protected /* var */manaSlider: ControlSlider;

        protected /* var */labelTip: Tooltip;

        protected /* var */buffPanel: BuffPanel;

        protected /* var */entityInfo: SEntityInfo;

        protected /* var */autoResumeSetType: string;

        protected /* var */isShowHealthLabelTip: Boolean=true;

        protected /* var */isShowManaLabelTip: Boolean=true;

        protected /* var */isShowHealthText: Boolean=true;

        protected /* var */isShowManaText: Boolean=true;

        /* internal */ /* var */_mainBgName: string="RoleAvatarbg";

        /* internal */ /* var */_avatarName: string="";

        /* internal */ /* var */_isHealthOver: Boolean=false;

        /* internal */ /* var */_isManaOver: Boolean=false;

        protected /* var */closeBtn: Object /* flash.display.SimpleButton */;
    }
